package com.neu.credit.creditscore.controller;

import com.neu.credit.creditscore.common.utils.Result;
import com.neu.credit.creditscore.dto.res.book.BookPreOrderInfoRes;
import com.neu.credit.creditscore.dto.res.book.BookRecordRes;
import com.neu.credit.creditscore.dto.res.book.BooksListingRes;
import com.neu.credit.creditscore.dto.res.book.BooksPageListRes;
import com.neu.credit.creditscore.entity.books.Books;
import com.neu.credit.creditscore.service.books.BooksService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/book")
@Tag(name = "图书馆管理", description = "图书借阅、查询、归还等相关接口") // 替换 @Api
@Slf4j
public class BooksController {

    @Autowired
    private BooksService booksService;

    @GetMapping("/recommend")
    @Operation(summary = "获取默认推荐的书籍列表") // 替换 @ApiOperation
    public Result<List<Books>> getRecommendBook() {
        try {
            return booksService.getRecommendBook();
        } catch (Exception e) {
            log.error("getRecommendParking has a error", e);
            return Result.error("获取默认推荐的停车场列表失败！");
        }
    }

    @GetMapping("/list")
    @Operation(summary = "获取书籍分页列表", description = "支持按分类和标题筛选")
    public Result<BooksPageListRes> getBooksPage(
            @Parameter(description = "页码，默认为1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量，默认为10") @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
            @Parameter(description = "分类ID，0表示全部") @RequestParam(value = "categoryId", defaultValue = "0") int categoryId,
            @Parameter(description = "搜索标题关键字") @RequestParam(value = "title", defaultValue = "") String title
    ) {
        try {
            return booksService.getBooksPage(pageNum, pageSize, categoryId, title);
        } catch (Exception e) {
            log.error("getBooksPage has a error", e);
            return Result.error("获取书籍分页列表失败！");
        }
    }


    @PostMapping("/admin/books/save")
    @Operation(summary = "添加图书")
    public Result<String> saveBook(@RequestBody Books books ) {
        try {
            return booksService.saveBook(books );
        } catch (Exception e) {
            log.error("saveBook has a error", e);
            return Result.error("获取书籍详情失败！");
        }
    }

    @GetMapping("/admin/books/deleted")
    @Operation(summary = "删除图书")
    public Result<String> deletedBook(@RequestParam("bookId") int bookId) {
        try {
            return booksService.deletedBook(bookId);
        } catch (Exception e) {
            log.error("deletedBook has a error", e);
            return Result.error("删除书籍失败！");
        }
    }

    @PostMapping("/admin/books/update")
    @Operation(summary = "修改图书")
    public Result<String> updateBook(@RequestBody Books books ) {
        try {
            return booksService.updateBook(books );
        } catch (Exception e) {
            log.error("updateBook has a error", e);
            return Result.error("修改书籍失败！");
        }
    }

    @GetMapping("/books/detail/{bookId}")
    @Operation(summary = "获取书籍详情")
    public Result<Books> getBooksDetail(
            @Parameter(description = "书籍ID", required = true) @PathVariable("bookId") int bookId
    ) {
        try {
            return booksService.getBooksDetail(bookId);
        } catch (Exception e) {
            log.error("getBooksDetail has a error", e);
            return Result.error("获取书籍详情失败！");
        }
    }

    @GetMapping("/listing")
    @Operation(summary = "获取借阅清单", description = "获取当前用户的借阅清单")
    public Result<BooksListingRes> getBookListing(@RequestParam("id") Integer id) {
        System.out.println("获取清单");
        System.out.println("用户: "+id);
        try {
            return booksService.getBookListing(id);
        } catch (Exception e) {
            log.error("getBookListing has a error", e);
            return Result.error("获取书籍借阅清单失败！");
        }
    }

    @GetMapping("/listing/add/{bookId}")
    @Operation(summary = "添加借阅清单", description = "将书籍添加到借阅清单")
    public Result<String> addBookListing(
            @Parameter(description = "书籍ID", required = true) @PathVariable("bookId") int bookId,@RequestParam("id") Integer id
    ) {
        System.out.println("添加借阅清单");
        System.out.println(bookId);
        System.out.println(id);
        try {
            return booksService.addBookListing(bookId,id);
        } catch (Exception e) {
            log.error("addBookListing has a error", e);
            return Result.error("添加借阅清单失败！");
        }
    }

    @GetMapping("/listing/deleted/{bookId}")
    @Operation(summary = "删除借阅清单", description = "从借阅清单中移除书籍")
    public Result<String> deteledBookListing(
            @Parameter(description = "书籍ID", required = true) @PathVariable("bookId") int bookId,@RequestParam("id") Integer id
    ) {
        try {
            return booksService.deteledBookListing(bookId,id);
        } catch (Exception e) {
            log.error("deteledBookListing has a error", e);
            return Result.error("删除借阅清单失败！");
        }
    }

    @GetMapping("/pre/order")
    @Operation(summary = "预下单", description = "生成借阅预订单")
    public Result<BookPreOrderInfoRes> preOrderListing(@RequestParam("id") Integer id) {
        try {
            return booksService.preOrderListing(id);
        } catch (Exception e) {
            log.error("preOrderListing has a error", e);
            return Result.error("预下单失败！");
        }
    }

    @GetMapping("/pay/order/{bookReturnFeeId}")
    @Operation(summary = "支付下单", description = "确认借阅并支付")
    public Result<String> payOrderListing(
            @Parameter(description = "借阅费用ID", required = true) @PathVariable("bookReturnFeeId") int bookReturnFeeId
    ) {
        try {
            return booksService.payOrderListing(bookReturnFeeId );
        } catch (Exception e) {
            log.error("preOrderListing has a error", e);
            return Result.error("支付下单失败！");
        }
    }

    @GetMapping("/record")
    @Operation(summary = "我的借阅记录", description = "查询历史借阅记录")
    public Result<BookRecordRes>   borrowingsRecord(@RequestParam("id") Integer id) {
        try {
            return booksService.borrowingsRecord(id);
        } catch (Exception e) {
            log.error("borrowingsRecord has a error", e);
            return Result.error("获取借阅记录失败！");
        }
    }

    @GetMapping("/return")
    @Operation(summary = "归还书籍", description = "归还已借阅的书籍")
    public Result<String> returnBookFee(@RequestParam("bookId") Integer bookId,@RequestParam("borrowingsId") Integer borrowingsId) {
        try {
            return booksService.returnBookFee(   bookId,  borrowingsId);
        } catch (Exception e) {
            log.error("returnBookFee has a error", e);
            return Result.error("归还借阅书籍失败！");
        }
    }
}